package com.gxitsky.redis;

import com.alibaba.fastjson.JSON;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author gxing
 * @desc TODO
 * @date 2022/8/2
 */
public class RedisKeyAnalyzeTool {

    private static List<String> fileList = Arrays.asList("D:\\rediskey\\redis_key144_7001.txt",
            "D:\\rediskey\\redis_key145_7001.txt", "D:\\rediskey\\redis_key146_7001.txt");

//    private static List<String> fileList = Arrays.asList("D:\\rediskey\\redis_key144_7001.txt");

    private static List<String> keyList = new ArrayList<>();

    private static HashMap<String, Integer> keyMap = new HashMap<>();


    public static void main(String[] args) {

        fileList.forEach(e -> {
            try {
                FileReader fileReader = new FileReader(e);
                BufferedReader bufferedReader = new BufferedReader(fileReader);
                bufferedReader.lines().forEach(line -> {
                    String[] split = new String[0];
                    String symbol = null;
                    String key = null;

                    // key以user:为前缀
                    if (line.startsWith("user:")) {
                        split = line.split(":");
                        symbol = ":";
                        key = split[0] + symbol + split[1];
                    } else if (line.startsWith("S:C:") || line.startsWith("S:B:") || line.startsWith("S:L:") || line.startsWith("S:N:")
                            || line.startsWith("SCHEDULE:BOOK:") || line.startsWith("SCHEDULE:CMP:") || line.startsWith("alipay:accessToken:")
                            || line.startsWith("SCHEDULE:NUM:")) {
                        split = line.split(":");
                        symbol = ":";
                        key = split[0] + symbol + split[1];
                    } else if (line.startsWith("futian_feesub_")) {
                        split = line.split("_");
                        symbol = "_";
                        key = split[0] + symbol + split[1];
                    } else if (line.startsWith("RefreshHisExamineReportListCommand:")) {
                        split = line.split(":");
                        symbol = ":";
                        key = split[0] + symbol;
                    } else if (line.startsWith("REPORT_INSP_LIST:")) {
                        split = line.split(":");
                        symbol = ":";
                        key = split[0] + symbol;
                    } else if (line.contains(":")) {
                        split = line.split(":");
                        symbol = ":";
                        if (split.length > 3) {
                            key = split[0] + symbol + split[1] + symbol + split[2];
                        } else if (split.length == 3) {
                            key = split[0] + symbol + split[1];
                        } else if (split.length > 0) {
                            key = split[0];
                        }
                    } else if (line.contains("_")) {
                        split = line.split("_");
                        symbol = "_";
                        if (split.length > 3) {
                            key = split[0] + symbol + split[1] + symbol + split[2];
                        } else if (split.length == 3) {
                            key = split[0] + symbol + split[1];
                        } else if (split.length > 0) {
                            key = split[0];
                        }
                    }

                    if (keyList.contains(key)) {
                        keyMap.put(key, keyMap.get(key) + 1);
                    } else {
                        keyList.add(key);
                        keyMap.put(key, 1);
                    }

                });
            } catch (IOException ex) {
                throw new RuntimeException(ex);
            }
        });

        keyMap.remove(null);
        System.out.println(JSON.toJSONString(keyMap));
        List<Map.Entry<String, Integer>> collect1 = keyMap.entrySet().stream().sorted(Map.Entry.comparingByValue())
                .collect(Collectors.toList());
        List<String> keyList1 = new ArrayList<>();
        List<Integer> valueList1 = new ArrayList<>();

        collect1.forEach(e -> {
            keyList1.add(e.getKey());
            valueList1.add(e.getValue());
        });
        System.out.println(JSON.toJSONString(keyList1));
        System.out.println(JSON.toJSONString(valueList1));
    }
}

